//
//  ByUIMineIndexVC.m
//  chequanbao_ios
//
//  Created by pro on 17/5/6.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIMineIndexVC.h"

// cell
#import "ByCellMine.h"
#import "ByCellMineLast.h"
// view
#import "ByViewMineHeader.h"


// api
#import "ByApiMine.h"

// vc
#import "ByUIMineSettingsVC.h"              // 设置
@interface ByUIMineIndexVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) ByViewMineHeader *headerView;

// data
@property (nonatomic, strong) NSArray  *titlesAry;
@property (nonatomic, strong) NSArray  *imgAry;
//@property (nonatomic, copy)   NSString *person;
@end

@implementation ByUIMineIndexVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人中心";
    [self addBYItemviews:@[@"mine_settings"] mode:ByNavigationItemModeImage position:ByNavigationItemPositionModeRight];
    [self initTableview];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    ByModelUser *user = [ByCacheUser getUserInfo];
    _headerView.userModel = user;
    [self.table setContentOffset:CGPointMake(0,0) animated:NO];
    if (user) {
        [self fetchData];
        [self fetchCountInfo];
    }else{
        user.personCount = @"0";
        [ByCacheUser setUserInfo:user];
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:3];
        [self.table reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
    }

}

- (void)viewSafeAreaInsetsDidChange{
    
    [super viewSafeAreaInsetsDidChange];
    _headerView.top = self.view.safeAreaInsets.top;
    _table.top = _headerView.bottom;
    _table.height = self.view.height - 160 - self.view.safeAreaInsets.bottom - 49 - self.view.safeAreaInsets.top;
}
#pragma mark -- 获取数据
// 获取积分
- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.uid = BY_USER.uid;
    model.code_type = BY_USER.account_id;
    [ByApiMine getMinePoints:model success:^(ByNetBaseResult *result) {
        ByModelUser *user = [ByCacheUser getUserInfo];
        user.points = [result.data objectForKey:@"score1"];
        [ByCacheUser setUserInfo:user];
        _headerView.userModel = user;
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}
// 获取自身统计
- (void)fetchCountInfo{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.account_id = BY_USER.uid;
    model.account_type = @"6";
    [ByApiMine fetchCountInfo:model success:^(ByNetBaseResult *result) {
        ByModelUser *user = [ByCacheUser getUserInfo];
        user.personCount = result.data[@"member_count"];
        [ByCacheUser setUserInfo:user];
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:3];
        [self.table reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
}

#pragma mark -- 设置
-(void)rightItemsAction:(NSInteger)rightIndex{
    if (!BY_USER) {
        ByUILoginVC *vc = [[ByUILoginVC alloc] init];
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self presentViewController:navi animated:YES completion:nil];
        return;
    }
    ByUIMineSettingsVC *vc = [[ByUIMineSettingsVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}

#pragma mark -- 懒加载
-(NSArray *)titlesAry{
    if (!_titlesAry) {
        _titlesAry = @[@[@"我的二维码",@"历史记录"],@[@"我的福利",@"我的积分"],@[@"我的消费",@"推广中心"]];
    }
    return _titlesAry;
}

-(NSArray *)imgAry{
    if (!_imgAry) {
         _imgAry = @[@[@"mine_qr_code",@"mine_insurance"],@[@"mine_fuli",@"mine_points"],@[@"mine_consume",@"mine_extend"]];
    }
    return _imgAry;
}
#pragma mark -- 创建视图
- (void)initTableview{
    _headerView = [[ByViewMineHeader alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 160)];
    _headerView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_headerView];
    
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, _headerView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - _headerView.height - 64) style:UITableViewStylePlain];
    _table.separatorStyle = UITableViewCellSelectionStyleNone;
    _table.delegate = self;
    _table.dataSource = self;  
    _table.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_table];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titlesAry.count + 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section < self.titlesAry.count) {
        ByCellMine *cell = [ByCellMine cellWithTableView:tableView titlesAry:self.titlesAry[indexPath.section] imgAry:self.imgAry[indexPath.section] indexPath:indexPath.section];
        return cell;
    }else{
        ByCellMineLast *cell = [ByCellMineLast cellWithTableView:tableView indexPath:indexPath.section];
        if (BY_USER.personCount) {
            cell.person = BY_USER.personCount;
        }else{
            cell.person = @"0";
        }
        return cell;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = SCREEN_WIDTH - 2*10;
    return  width/3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

@end
